package com.kornero.auto.view.groups;

import com.kornero.auto.util.view.LayoutManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AutoUpdateGroupImpl extends Composite implements IAutoUpdateGroup {

    private static final Logger logger = LoggerFactory.getLogger(AutoUpdateGroupImpl.class);

    private static final String STATUS = "Status: ";
    private static final String TIMEOUT = "120";

    private Label status;
    private Text intervalValue;
    private Button start, stop;

    private boolean stopped = true;

    public AutoUpdateGroupImpl(Composite parent) {
        this(parent, SWT.NONE);
    }

    public AutoUpdateGroupImpl(Composite parent, int style) {
        super(parent, style);
        this.constructUI();
    }

    private void constructUI() {
        this.setLayout(new FillLayout());
        this.setLayoutData(LayoutManager.getLayoutDataFillVertical());

        final Group group = new Group(this, SWT.NONE);
        group.setLayout(LayoutManager.getRowVerticalLayout());
        group.setText("AutoUpdate");

        this.status = new Label(group, SWT.NONE);
        this.status.setText(STATUS);
        this.status.pack();

        final Composite composite1 = new Composite(group, SWT.NONE);
        composite1.setLayout(new RowLayout());

        new Label(composite1, SWT.NONE).setText("Interval:  ");
        this.intervalValue = new Text(composite1, SWT.BORDER);
        this.intervalValue.setText(TIMEOUT);
        new Label(composite1, SWT.NONE).setText("  seconds.   ");

        final Composite composite2 = new Composite(group, SWT.NONE);
        composite2.setLayout(new FillLayout());

        final Listener listener = createListener();
        this.start = createStartButton(composite2, listener);
        this.stop = createStopButton(composite2, listener);

        group.pack();
    }

    private Listener createListener() {
        return new Listener() {
            @Override
            public void handleEvent(Event event) {
                stopped = !stopped;

                start.setEnabled(stopped);
                stop.setEnabled(!stopped);
            }
        };
    }

    private Button createStartButton(final Composite composite, final Listener listener) {
        final Button start = new Button(composite, SWT.NONE);
        start.setText("Start");
        start.setEnabled(stopped);
        start.addListener(SWT.MouseDown, listener);
        return start;
    }

    private Button createStopButton(final Composite composite, final Listener listener) {
        final Button stop = new Button(composite, SWT.NONE);
        stop.setText("Stop");
        stop.setEnabled(!stopped);
        stop.addListener(SWT.MouseDown, listener);
        return stop;
    }

    @Override
    public void addStartListener(Listener listener) {
        this.start.addListener(SWT.MouseDown, listener);
    }

    @Override
    public void addStopListener(Listener listener) {
        this.stop.addListener(SWT.MouseDown, listener);
    }

    @Override
    public void setStatus(String textStatus) {
        this.status.setText(STATUS + textStatus);
        this.status.pack();
    }

    @Override
    public int getTimeout() {
        try {
            final int timeout = Integer.parseInt(this.intervalValue.getText());
            if (timeout > 0) {
                return timeout;
            }
        } catch (NumberFormatException e) {
            logger.error("getTimeout(): Bad number = '" + this.intervalValue.getText() + "'");
        }

        // One minute by default.
        return 60;
    }
}
